//
//  OMTSDKImageCache.m
//  Pods
//
//  Created by yizhaorong on 2017/3/20.
//
//

#import "OMTImageCache.h"
#import "OMTAutoPurgaCache.h"


FOUNDATION_STATIC_INLINE NSUInteger OMTCacheCostForImage(UIImage *image) {
    return image.size.height * image.size.width * image.scale * image.scale;
}

@interface OMTImageCache ()

@end

@implementation OMTImageCache

+ (instancetype)sharedInstance {
    static OMTImageCache *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [OMTImageCache new];
    });
    return instance;
}

- (instancetype)init {
    if (self = [super init]) {
        self.memCache.name = @"com.onemt.image";
    }
    return self;
}

- (void)storeImage:(UIImage *)image forKey:(NSString *)key {
    if (!image || !key) {
        return;
    }
    
    NSUInteger cost = OMTCacheCostForImage(image);
    [self.memCache setObject:image forKey:key cost:cost];
}

- (UIImage *)imageForKey:(NSString *)key {
    return [self.memCache objectForKey:key];
}

@end
